import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:whale/helper/utils/util.dart';

class PopPage extends StatefulWidget {
  const PopPage({
    super.key,
    this.t,
  });
  final String? t;
  @override
  State<PopPage> createState() => _PopPageState();
}

class _PopPageState extends State<PopPage> {
  double offx = 0;
  double startX = 0;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onHorizontalDragStart: (details) {
        startX = details.localPosition.dx;
      },
      onHorizontalDragUpdate: (details) {
        setState(() {
          offx = details.localPosition.dx - startX;
        });
      },
      onHorizontalDragCancel: () {},
      onHorizontalDragEnd: (details) {
        print(details);
        if (offx > 50) {
          Navigator.of(context).pop();
        } else {
          setState(() {
            offx = 0;
          });
        }
      },
      child: Stack(
        // fit: StackFit.passthrough,
        children: [
          Positioned(
            left: offx,
            right: 0,
            top: 0,
            bottom: 0,
            child: Scaffold(
              appBar: detailAppBar(context, title: 'test'),
              body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(widget.t ?? '空'),
                  TextButton(
                      onPressed: () {
                        Navigator.of(context).pushReplacement(
                          PageRouteBuilder(
                            pageBuilder:
                                (context, animation, secondaryAnimation) {
                              return PopPage(
                                t: '第三级页面',
                              );
                            },
                            opaque: false,
                            transitionsBuilder: (context, animation,
                                secondaryAnimation, child) {
                              return FadeTransition(
                                opacity: Tween<double>(begin: 0, end: 1)
                                    .animate(animation),
                                child: child,
                              );
                            },
                          ),
                        );
                      },
                      child: Text(
                        'push',
                        style: TextStyle(color: Colors.amber),
                      )),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
